<?php

namespace app\controller;

use app\BaseController;
use app\common\model\CouponModel;
use app\common\model\PlatformStatisticsModel;
use app\common\model\UserModel;
use app\common\model\WebMenuModel;
use app\common\service\Config;
use think\App;
use think\facade\Lang;
use think\facade\View;

class Common extends BaseController
{
    /**
     * @var UserModel|null
     * */
    protected $user_model=null;

    public function initialize()
    {
        PlatformStatisticsModel::statistics();
        $this->app->lang->setLangSet('en-us');
        parent::initialize(); // TODO: Change the autogenerated stub


        // 模板变量赋值
        $coupon_tip_model = CouponModel::where(['status'=>1,'is_index'=>1])->find();
        View::assign('coupon_tip_model',$coupon_tip_model);
        View::assign('user_model',$this->user_model);
        View::assign('money_f','$');
        View::assign('money_f_name','USD');
        View::assign('include_goods_num',0);
    }


    public function __call($method, $args)
    {
        return $this->miss();
    }


    public function miss()
    {

        //模板后缀
        $view_suffix = '.' . config('view.view_suffix');

        $base_url = $this->request->baseUrl();
        $base_url_arr = explode('.', $base_url);
        count($base_url_arr) > 1 && array_pop($base_url_arr);
        $base_url = implode('', $base_url_arr);

        $base_url = $base_url === '/' ? '/index' : $base_url;
        $temp_def_path = 'app/view';
        $root_path = root_path($temp_def_path);
        $root_path_temp = WebMenuModel::VIEW_ROOT;
        $page_path = $base_url . $view_suffix;

        $path_info = pathinfo($this->request->pathinfo());
        $extension = $path_info['extension'] ?? '';
        //        dump($root_path,$page_path);exit;
        if (file_exists($root_path . $page_path)) {
            return view($base_url, [
                'demo_time' => date('Y-m-d H:i:s'),
                'user_model' => $this->user_model,
            ]);
        }elseif(file_exists($root_path_temp . $page_path)){
            return view(str_replace($temp_def_path,'',WebMenuModel::VIEW_ROOT).$base_url, [
                'demo_time' => date('Y-m-d H:i:s'),
                'user_model' => $this->user_model,
            ]);
        } else {

            if ($extension == 'html') {
                return view('/error/miss', [
                    'page' => $page_path,
                    'user_model' => $this->user_model,
                ]);
            } else {
                return $this->_resData(0, '资源不存在');
            }
        }
    }



    /**
     * 生产登录凭证
     * @param $model UserModel
     * @return array
     * */
    protected function loginInfo(UserModel $model): array
    {
        return ['token' => $model->generateUserToken($model['password'])];
    }
}
